import Vue from 'vue'
import Vuex from 'vuex'
import utilHelper from '../config'
import Utils from '../js/utils'
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        freshSelectUserData: '',
        selectUserDataType: '',
        selectUserDataChiefName: '',
        selectUserDataChiefPhone: '',
        selectUserDataManagerName: '',
        selectUserDataManagerPhone: '',
        selectRoleInfo: '',
        selectOldRoleInfo: '',
        premisesName: '',
        projectCode: '',
        parentList:[],
        subNodeList:[]
    },
    mutations: {
        // setPremisesData(state,data){
        //     state.premisesData = data  
        // },
        set_freshSelectUserData(state, freshSelectUserData) {
            state.freshSelectUserData = freshSelectUserData
        },
        set_selectUserDataType(state, selectUserDataType) {
            state.selectUserDataType = selectUserDataType
        },
        set_selectUserDataChiefName(state, selectUserDataChiefName) {
            state.selectUserDataChiefName = selectUserDataChiefName
        },
        set_selectUserDataChiefPhone(state, selectUserDataChiefPhone) {
            state.selectUserDataChiefPhone = selectUserDataChiefPhone
        },
        set_selectUserDataManagerName(state, selectUserDataManagerName) {
            state.selectUserDataManagerName = selectUserDataManagerName
        },
        set_selectUserDataManagerPhone(state, selectUserDataManagerPhone) {
            state.selectUserDataManagerPhone = selectUserDataManagerPhone
        },
        set_selectRoleInfo(state, selectRoleInfo) {
            state.selectRoleInfo = selectRoleInfo
        },
        set_selectOldRoleInfo(state, selectOldRoleInfo) {
            state.selectOldRoleInfo = selectOldRoleInfo
        },
        set_premisesName(state, premisesName) {
            state.premisesName = premisesName
        },
        set_projectCode(state, projectCode) {
            state.projectCode = projectCode
        },
    },
    actions:{
        getMenuList({ commit,state }, ){
            return new Promise((resolve,reject)=>{
                var url = utilHelper.apiUrl + "/api/getMenu";
			    var body = {};
			    var params = {
                    resourcesType: "1",
			    	resourcesOrigin: "1",
			    	resourcesLevel: "0"
                }
			    body.params = params;
			    Vue.http.post(url, body).then((response)=>{
			    	if(response.body.resData && response.body.resData.resourcesMap){
			    		state.parentList = response.body.resData.resourcesMap.parent;
			    		state.subNodeList = response.body.resData.resourcesMap.subNode;
                        var firstHref = "";
                        if(state.parentList){
                            for(var ii=0; ii<state.parentList.length; ii++){
                                var menu = state.parentList[ii];
                                var resourcesHref = menu.resourcesHref;
                                if("#" != resourcesHref){
                                    firstHref = resourcesHref;
                                    break;
                                }
                            }
                        }
                        if(!firstHref && state.subNodeList){
                            for(var ii=0; ii<state.subNodeList.length; ii++){
                                var menu = state.subNodeList[ii];
                                var resourcesHref = menu.resourcesHref;
                                if("#" != resourcesHref){
                                    firstHref = resourcesHref;
                                    break;
                                }
                            }
                        }
			    		console.log(firstHref,9090);
                        resolve(firstHref)
			    	}else {
                        window.localStorage.setItem("1_isLogin", false);
                        Utils.showStatusMsg("您没有访问权限,请联系管理员!", "error");
			    		_self.$router.replace({
			    			path: '/login'
			    		})
			    	}
                }),(err)=>{
    
                }                
            })
			
        }
    },
    getters: {
        freshSelectUserData(state) {
            return state.freshSelectUserData
        },
        selectUserDataType(state) {
            return state.selectUserDataType
        },
        selectUserDataChiefName(state) {
            return state.selectUserDataChiefName
        },
        selectUserDataChiefPhone(state) {
            return state.selectUserDataChiefPhone
        },
        selectUserDataManagerName(state) {
            return state.selectUserDataManagerName
        },
        selectUserDataManagerPhone(state) {
            return state.selectUserDataManagerPhone
        },
        selectRoleInfo(state) {
            return state.selectRoleInfo
        },
        selectOldRoleInfo(state) {
            return state.selectOldRoleInfo
        },
        premisesName(state) {
            return state.premisesName
        },
        projectCode(state) {
            return state.projectCode
        },
        // getPremisesData(state){
        //     return state.premisesData
        // },
        parentList(state){
            return state.parentList
        },
        subNodeList(state){
            return state.subNodeList
        },
    }
})